package com.devframe.sys.util.cglib;
/**
 * 版权所有：福建邮科电信业务部厦门研发中心 
 *====================================================
 * 文件名称: MyBeanCopierContainer.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2013-1-12			詹保山（softzbs@126.com）(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;

/**
 * 
 *<pre><b><font color="blue">MyBeanCopierContainer</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   MyBeanCopierContainer obj = new MyBeanCopierContainer();
 *   obj.method();
 * </pre>
 * @author  <b>詹保山</b>
 */
public class MyBeanCopierContainer   {
	
	private static Log log=LogFactory.getLog(MyBeanCopierContainer.class);
	
	private  ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	private static final String RESOURCE_PATTERN = "/**/*.class";
	private List<String> packagesToScanList=new ArrayList<String>();
	private static ConcurrentHashMap<String ,MyBeanCopier> MyBeanCopyContainerMap=new ConcurrentHashMap<String,MyBeanCopier>();
	
	public void init(){
		log.info("创建模型的BeanCopier------>start");
		try {
			for (String pkg : packagesToScanList) {
				String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
				Resource[] resources = resourcePatternResolver.getResources(pattern);
				MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
				for (Resource resource : resources) {
					if (resource.isReadable()) {
						MetadataReader reader = readerFactory.getMetadataReader(resource);
						String className = reader.getClassMetadata().getClassName();
						Class clazz=Class.forName(className);
						MyBeanCopier myBeanCopier=MyBeanCopier.create(clazz, clazz, true);
						
						MyBeanCopyContainerMap.put(className, myBeanCopier);
					}
				}
			}
			log.info("创建模型的BeanCopier----->end;size="+MyBeanCopierContainer.MyBeanCopyContainerMap.size());
		}
		catch (IOException ex) {
			log.error("MY BEAN COPY 初始化失败");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			log.error(e);
		}
	}




 
	
	private static synchronized MyBeanCopier createMyBeanCopier(Class clazz){
		MyBeanCopier beanCopier=MyBeanCopyContainerMap.get(clazz.getName());
		if(beanCopier==null){
			 beanCopier=MyBeanCopier.create(clazz, clazz, true);
			MyBeanCopyContainerMap.put(clazz.getName(), beanCopier);
		}
		return beanCopier;
	}
	
	public static MyBeanCopier accessBeanCopier(Class clazz){
		MyBeanCopier beanCopier=MyBeanCopyContainerMap.get(clazz.getName());
		if(beanCopier==null){
			beanCopier=createMyBeanCopier(clazz);
		}
		return beanCopier;
	}




	public void setPackagesToScanList(List<String> packagesToScanList) {
		this.packagesToScanList = packagesToScanList;
	}
}
